package com.lj.tools.util;

import com.lj.tools.exception.YerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import java.util.regex.Pattern;

public class XMLUtil {
    static Logger log = LoggerFactory.getLogger(XMLUtil.class);

    public static boolean isBucketNameValidDNSName(String bucketName) {
        if (bucketName != null && bucketName.length() <= 63 && bucketName.length() >= 3) {
            if (!Pattern.matches("^[a-z0-9][a-z0-9.-]+$", bucketName)) {
                return false;
            } else if (Pattern.matches("(\\d{1,3}\\.){3}\\d{1,3}", bucketName)) {
                return false;
            } else {
                String[] fragments = bucketName.split("\\.");
                String[] var2 = fragments;
                int var3 = fragments.length;

                for(int var4 = 0; var4 < var3; ++var4) {
                    String fragment = var2[var4];
                    if (Pattern.matches("^-.*", fragment) || Pattern.matches(".*-$", fragment) || Pattern.matches("^$", fragment)) {
                        return false;
                    }
                }

                return true;
            }
        } else {
            return false;
        }
    }

    public static String generateHostnameForBucket(String bucketName, boolean pathStyle, String endpoint) {
        if (!isBucketNameValidDNSName(bucketName)) {
            throw new IllegalArgumentException("the bucketName is illegal");
        } else {
            return !pathStyle ? bucketName + "." + endpoint : endpoint;
        }
    }

    public static XMLReader loadXMLReader() throws YerException {
        try {
            XMLReader reader = XMLReaderFactory.createXMLReader();

            try {
                reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
                reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
                reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
                reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
            } catch (Exception var9) {
                log.warn("Enable protection for XML External Entity Injection Failed");
            }

            return reader;
        } catch (Exception var11) {
            String[] altXmlReaderClasspaths = new String[]{"org.apache.crimson.parser.XMLReaderImpl", "org.xmlpull.v1.sax2.Driver"};
            String[] var2 = altXmlReaderClasspaths;
            int var3 = altXmlReaderClasspaths.length;
            int var4 = 0;

            while(var4 < var3) {
                String xmlReaderClasspath = var2[var4];

                try {
                    XMLReader reader = XMLReaderFactory.createXMLReader(xmlReaderClasspath);

                    try {
                        reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
                        reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
                        reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
                        reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
                    } catch (Exception var8) {
                        log.warn("Enable protection for XML External Entity Injection Failed");
                    }

                    return reader;
                } catch (Exception var10) {
                    ++var4;
                }
            }

            throw new YerException("Failed to initialize a SAX XMLReader", var11);
        }
    }
}
